﻿var loginUserId;
var loginUserName;

var page1 = new Sail.page({
    name     : "mainList",                  //定义页面对象的唯一标识 名称
    mode     : 'list',                      //这只页面对象的运行模式：列表页面
	isIframe : true,
    
    frames   : {
        name     : "listFrame",                 //该frame对象的唯一标识
        title    : "数据列表",             //标题文本

        //-------------------------------------------------------------------------------
        //以JSON形式描述 frame 的构成(包含哪些 item)
        //-------------------------------------------------------------------------------
        items    : [
                {
                name  : "search",   
                type  : "form", 
                title : "查询条件",
                "isSearch" : true,
                "items" : [
                {
                  type:"calendar", 
                  title:"单据日期从", 
                  dataIndex:'beginDay'
                },{
                  type:"calendar", 
                  title:"至", 
                  dataIndex:'endDay'
                },{
                  type:"input", 
                  title:"空调名称",   
                  dataIndex:'masterName'
                },{
                  type:"combox", 
                  title:"供应商",
                  dataIndex : "supplierId",
                  nameIndex : "supplierName",    
					        allowBlank:true,
					        root:"dataset.item",
					        fieldText:"name",
					        fieldValue:"id",
					        url:"../../../DataDict?type=repairsSupplier"
                },{
        					type:"combox", title:"资金形式",
        					dataIndex : "filter_EQS_otherDic1.id",  
        					allowBlank:true,
        					root:"dataset.dictionary",
        					fieldText:"name",
        					fieldValue:"id",
        					url: "../../dictionary.do?method=findDictionaryByTypeId&typeId="+COMBOX_PRICE_TYPE
                },{
        					type:"combox", title:"录入人",
        					dataIndex : "creatorId",
        					nameIndex : "creatorName",    
        					allowBlank:true,
        					root:"dataset.item",
        					fieldText:"name",
        					fieldValue:"id",
        					url:""
                }
		            ],
                "layoutConfig" : {
                    columns : 4
                },
                "layout" : [
                    {
                	  items:[
			                {'name':'单据日期从'},
			          	    {'name':'至'},
			          	    {'name':'供应商'},
			          	    {'name':'资金形式'}
                           ]
                    },
                    {
                  	  items:[
  			          	    {'name':'空调名称'},
  			          	    {'name':'录入人'}
                             ]
                     }
                ]
            },
            {
                name      : "gridMain",
                type      : "grid",
                title     : "空调进货单列表",
                tbar      : ['新增','-','删除','-','复制','-'],
                pageSize:12,
            	  singleSelect:false,
            	  height:MAIN_HEIGHT,
                bindRelation   : {
                    root  :"dataset.billVo",
                    id    : "id",
                    field :["transactionId","creatorId","billStatus"],
                    col   :[
                        {"header":"单据日期", "width":80,   "dataIndex":"billDate","renderer":Sail.formatDate},
                        {"header":"单据编号", "width":100,   "dataIndex":"billCode"},
                        {"header":"供应商",   "align":"left","width":200,   "dataIndex":"otherDic2Name" },
                        {"header":"资金形式", "align":"left","width":45,   "dataIndex":"otherDic1Name" },
                        //{"header":"仓库",     "align":"left","width":150,   "dataIndex":"otherDic3Name" },
                        {"header":"录入人",   "align":"left","width":60,   "dataIndex":"creatorName" },
                        {"header":"录入时间", "width":90,   "dataIndex":"createTime","renderer":Sail.toFullDate},
                        {"header":"修改",     "width":50,   "actionCol" :true, renderer:
                        	function(value,p,r){
	                   			  var creatorId = r.get('creatorId');
	                   			  var billStatus = r.get('billStatus');
	                   				return "<span class=actionSpan>修改</span>";
            				      }  
                        }
                    ]
                    
                }
            },
            {
                name      : "gridSub",
                type      : "grid",
                title     : "空调进货单明细",
                "colCheckbox"  : "false",
                bbar:false,
                haveCheckCol:false,
                height:SUB_HEIGHT,
                bindRelation   : {
                    root  :"dataset.billDetailVo",
                    col   :[
                        {"header":"供给形式",align:"left",  "width":80,   "dataIndex":"otherDic1Name" },
                        {"header":"名称规格",align:"left",  "width":200,   "dataIndex":"materielName" },
                        {"header":"单位",align:"left",  "width":60,   "dataIndex":"unitName" },
                        {"header":"单价",align:"left",  "width":80,   "dataIndex":"price" },
                        {"header":"数量",align:"left",  "width":70,   "dataIndex":"amount" },
                        {"header":"金额",align:"right",  "width":80,   "dataIndex":"money" }
                    ]
                    
                }
            }
        ],
        
        layoutConfig : {
            columns : 2,
            width:[0.55,0.45]
       	},
        
		layout : [
		    {
                items:[{'name':"search",'colspan':2}]
            },
            {
                items:[{'name':"gridMain"},{'name':"gridSub"}]
            }
        ],
        
        //-------------------------------------------------------------------------------
        //frame 的构成元素可以响应的事件的描述
        //此示例中，同时演示了查询条件中，多级 下拉列表联动 的实现
        //-------------------------------------------------------------------------------
        bindEvent2Items : function(){   
			//主、从grid
			var grid = $('gridMain');
			var subgrid = $('gridSub');
	
			//查询事件
			$('search').addButtonHandler('查询',function(){
				subgrid.removeAll();
				queryData();
		    });
			
			//重置
			$('search').addButtonHandler('重置',function(){
				setInitValue();
		    });
			
        //新增
        grid.addButtonHandler('新增',function(){
            Sail.open("purchaseDetail.html","add",{},MIDDLE_MODWIN_WIDTH,MIDDLE_MODWIN_HEIGHT);
            grid.load();
        });
            
          
        //删除
        grid.addButtonHandler('删除',function(){
          var ids = grid.getSelections('id').join(':');
          if (ids==''){
            return;
          }
          if(confirm("确认要删除所选择的单据吗？")==false){
            return;
          }
          Sail.postData('../../purchase.do?method=delete',
          {'ids':ids},
          function(flag,getData){
              if(flag == "true"){
                  Ext.Msg.alert("提示","操作成功!");
                  subgrid.removeAll();
                  grid.load();
              }
              else{
                  Ext.Msg.alert("警告",getData.dataset.response.content);
              }
          });
        });
            
        //修改
        grid.addAction('修改',function(id){
            Sail.open("purchaseDetail.html","update",{"id":id},MIDDLE_MODWIN_WIDTH,MIDDLE_MODWIN_HEIGHT);
            grid.load();
        });
            
        //复制
          grid.addButtonHandler('复制',function(){
          	var ids = grid.getSelections('id');
          	if (ids.length <= 0){
          		Ext.Msg.alert("提示","复制前，请选择一个单据作为样本！");
          		return;
          	}
          	
          	if (ids.length != 1){
          		Ext.Msg.alert("提示","复制新增时，您只能指定一个单据作为样本！");
          		return;
          	}
          	
             Sail.open("purchaseDetail.html","update",{"id":ids[0], "isCopyAdd":"true"},MIDDLE_MODWIN_WIDTH,MIDDLE_MODWIN_HEIGHT);
             grid.load();
          });
            
          //选择首行,显示子列表数据
          grid.on('afterload',function(){
              grid.selectFirstRow();
          });
            
          //主列表行事件,显示子列表数据
          grid.on('rowclick',function(id,row,record){
        		if (id == "") {
        			return;
        		}
        		var transactionId=record.get("transactionId");
        		if (transactionId == "") {
        			return;
        		}
          	var url = "../../purchase.do?method=listDetail&lastTransactionId="+transactionId;
          	$('gridSub').load(url);
          }); 
        }        
    },
    //进入页面自动查询数据
	afterRun : function(){
    	//设初值
    	setInitValue();
    	//查询数据
    	queryData();
	},
	
	afterLoadLoginInfo:function(){
		loginUserId = this.getUser();
		loginUserName = this.getOpername();
	}
});

page1.run();

function setInitValue(){
	//单据日期初始化
	var beginDate = getMonthFirstDay();
	$('单据日期从').setValue(beginDate);
	var endDate=getCurrentDay();
	$('至').setValue(endDate);
	
	if (!$('gridMain').getButton("新增").disabled)
	{	
		//$('录入人').setData(loginUserId, loginUserName);
	}
}

function queryData(){
	var searchInfo = $('search').getSearch();
	var url = "../../purchase.do?method=list&filter_EQS_billType.id="+BILL_TYPE_PURCHASE+searchInfo;
	$('gridMain').load(url);
}

